#!/bin/bash

# SmartInput 服务重启脚本
# 核心功能：停止然后启动所有服务。

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 项目根目录
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 打印带颜色的消息
print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

# 主函数
main() {
    print_message $GREEN "🔄 SmartInput 服务重启脚本"
    print_message $BLUE "================================"
    
    # 停止所有服务
    print_message $BLUE "🛑 停止现有服务..."
    if [ -f "$PROJECT_ROOT/stop.sh" ]; then
        bash "$PROJECT_ROOT/stop.sh"
    else
        print_message $RED "❌ 停止脚本不存在: $PROJECT_ROOT/stop.sh"
        exit 1
    fi
    
    print_message $YELLOW "⏳ 等待服务完全停止..."
    sleep 2
    
    # 启动所有服务
    print_message $BLUE "🚀 启动服务..."
    if [ -f "$PROJECT_ROOT/start.sh" ]; then
        bash "$PROJECT_ROOT/start.sh"
    else
        print_message $RED "❌ 启动脚本不存在: $PROJECT_ROOT/start.sh"
        exit 1
    fi
    
    print_message $BLUE "================================"
    print_message $GREEN "🎉 服务重启命令已发送！"
    print_message $YELLOW "请等待几秒钟让服务完全启动。"
    print_message $YELLOW "使用 ./status.sh 或 lsof -i :3001 来检查状态。"
}

# 执行主函数
main "$@"
